class TouchScale {
    constructor(element, scale) {
        this.element = element;
        if (scale) {
            this.prevScale = scale;
            this.currentScale = scale;
        } else {
            this.prevScale = 1;
            this.currentScale = 1;
        }

        this.isTouch = false;
    }

    gestureHandle(e) {
        // console.log(e.type);
        if (e.type === 'touchstart') {
            this.gestureStart(e);
        } else if (e.type === 'touchmove') {
            this.gestureMove(e);
        } else if (e.type === 'touchend') {
            this.gestureEnd(e);
        }
    }

    gestureStart(e) {
        //判断是否有两个点在屏幕上
        if (e.touches.length >= 2) {
            this.prevTouches = e.touches;
            this.isTouch = true;
            this.prevPointCenter = TouchScale.getPointCenter(e.touches[0], e.touches[1]);
            this.start = e.touches;  //得到第一组两个点
            this.onGestureStart(e); //执行gesturestart方法
        }
    }

    gestureMove(e) {
        let oTarget = e.target;
        let oJianPanLi = TouchScale.getParentJianPanLi(oTarget, 0);

        if (e.touches.length >= 2 && this.isTouch && !$(oJianPanLi).hasClass("keyBox")) {
            let currentTouches = e.touches;  //得到第二组两个点
            let horizontalDistance1 = 0;
            let horizontalDistance2 = 0;
            let VerticalDistance1 = 0;
            let VerticalDistance2 = 0;
            if (this.prevTouches) {
                horizontalDistance1 = this.prevTouches[0].pageX - currentTouches[0].pageX;
                horizontalDistance2 = this.prevTouches[1].pageX - currentTouches[1].pageX;
                VerticalDistance1 = this.prevTouches[0].pageY - currentTouches[0].pageY;
                VerticalDistance2 = this.prevTouches[1].pageY - currentTouches[1].pageY;
            }
            this.currentPointCenter = TouchScale.getPointCenter(currentTouches[0], currentTouches[1]);
            this.prevTouches = e.touches;
            //得到缩放比例，getDistance是勾股定理的一个方法
            let scale = TouchScale.getDistance(currentTouches[0], currentTouches[1]) / TouchScale.getDistance(this.start[0], this.start[1]);
            let rotation = TouchScale.getAngle(currentTouches[0], currentTouches[1]) - TouchScale.getAngle(currentTouches[0], currentTouches[1]);  //得到旋转角度，getAngle是得到夹角的一个方法
            e.scale = this.prevScale * Math.pow(scale, 0.15);
            this.currentScale = e.scale;
            this.currentScale = Math.min(Math.max(this.currentScale, 0.4), 2);
            e.rotation = rotation;
            this.currentRotation = e.rotation;
            this.onGestureMove(e);
            this.prevPointCenter = TouchScale.getPointCenter(currentTouches[0], currentTouches[1]);
        }
    }

    gestureEnd(e) {
        if (this.isTouch) {
            this.isTouch = false;
            this.onGestureEnd(e);
            this.prevScale = this.currentScale;
        }
    }

    onGestureStart() {
    };

    onGestureMove() {
    };

    onGestureEnd() {
    };

    static fixValue(value) {
        return Math.floor(value / 10) * 10;
    }

    static getDistance(p1, p2) {
        let x = this.fixValue(p2.pageX - p1.pageX),
            y = this.fixValue(p2.pageY - p1.pageY);
        return Math.sqrt((x * x) + (y * y));
    }

    static getAngle(p1, p2) {
        let x = p1.pageX - p2.pageX,
            y = p1.pageY - p2.pageY;
        return Math.atan2(y, x) * 180 / Math.PI;
    }

    static getPointCenter(p1, p2) {
        let x = (p2.pageX + p1.pageX) / 2,
            y = (p2.pageY + p1.pageY) / 2;
        return {x, y};
    }

    static getParentJianPanLi(target, count) {
        if (target && count < 10 && !$(target).hasClass('keyBox')) {
            return this.getParentJianPanLi(target.parentElement, count + 1);
        }
        return target;
    }
}

module.exports = {
    TouchScale
};
